/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import cn.ac.iie.di.global.GlobalParas;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author jzz
 */
public class JsonSplitor implements Runnable {

    String dir = null;
    String desdir = null;
    ArrayBlockingQueue dataQueue = null;
    String FileString = null;

    static Logger logger = null;

    static {
        logger = org.apache.log4j.Logger.getLogger(JsonSplitor.class.getName());
    }

    public JsonSplitor(String dir, String desdir, String filetype, ArrayBlockingQueue dataQueue) {
        this.dir = dir;
        this.dataQueue = dataQueue;
        this.desdir = desdir;
        this.FileString = filetype;
    }

    public void run() {
        while (true) {
            try {
                logger.info("the dir is " + dir);
                List<File> sigFiles = FileTools.getFiles(new File(dir), "sig");

                logger.info("current sig files " + sigFiles);
                for (File sigFile : sigFiles) {
                    String dataFilePath = sigFile.getPath().split(".sig")[0];
                    logger.info("file:" + dataFilePath);
                    readFileByLines(dataFilePath, desdir, FileString);
                }

                Thread.sleep(3000);
            } catch (Exception ex) {
                logger.error("The JsonSplitor is wrong " + ex, ex);
            }
        }
    }

    public void readFileByLines(String fileName, String newFileName, String FileType) {

        BufferedReader reader = null;
        String filetype = null;
        filetype = FileType;
        try {
            long begin = System.currentTimeMillis();
            //System.out.println("以行为单位读取文件内容，一次读一整行：");
            //reader = new BufferedReader(new FileReader(fileName));
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            String tempString = null;
            int line = 0;
            // 一次读入一行，直到读入null为文件结束  

            while ((tempString = reader.readLine()) != null) {
//				try {
                while (!dataQueue.offer(tempString)) {
//						Thread.sleep(1000);
                    //logger.warn("Put Json Array Block buffer Fail!");
                }

//				} catch (InterruptedException ex) {
//				}
                line++;
            }
            reader.close();
            logger.info(" Send data " + line + " using " + filetype + " in " + fileName + "！ ");
            //将文件移动走，删除.ok文件和解压文件，移动.gz和.sig文件
            //要是IMSG和GMSG直接移走，USER需要创建文件后缀
//			if (filetype.equals(GlobalParas.FILE_IMSG) || filetype.equals(GlobalParas.FILE_GMSG)) {
//				FileTools.moveFileToNewPath(fileName, newFileName);
//			} else if (filetype.equals(GlobalParas.FILE_USER)) {
//
//				//FileTools.moveUserFileToNewPath(fileName, newFileName);
//				FileTools.moveFileToNewPath(fileName, newFileName);
//			}
            FileTools.moveFileToNewPath(fileName, newFileName);
            long end = System.currentTimeMillis();
            logger.info("consume " + (end - begin) + " ms");
        } catch (Exception e) {
            logger.error("the error is :" + e, e);
        }
    }
}
